package cn.com.lyj6851.modules.mp.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.mp.domain.YdyWechatMenu;
import cn.com.lyj6851.modules.mp.service.YdyWechatMenuService;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatMenuDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatMenuQueryCriteria;
import cn.com.lyj6851.modules.mp.service.mapper.WechatMenuMapper;
import cn.com.lyj6851.utils.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;


@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "ydyWechatMenu")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdyWechatMenuServiceImpl extends BaseServiceImpl<WechatMenuMapper, YdyWechatMenu> implements YdyWechatMenuService {
    private final IGenerator generator;


    @Override
    public Map<String, Object> queryAll(YdyWechatMenuQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyWechatMenu> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdyWechatMenuDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<YdyWechatMenu> queryAll(YdyWechatMenuQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyWechatMenu.class,criteria));
    }

    @Override
    public void download(List<YdyWechatMenuDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyWechatMenuDto ydyWechatMenu : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("缓存数据", ydyWechatMenu.getResult());
            map.put("缓存时间", ydyWechatMenu.getAddTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    // 判重
    @Override
    public Boolean isExist(String wechat_menus) {
        YdyWechatMenu ydyWechatMenu = getOne(Wrappers.<YdyWechatMenu>lambdaQuery()
                .eq(YdyWechatMenu::getKey,wechat_menus));
        return ydyWechatMenu != null;
    }
}
